package licode.unisop.client.visit;

import licode.unisop.client.api.SopPlatform;
import licode.unisop.client.api.SopPlatformMng;
import licode.unisop.client.info.SopRealmIdIn;
import licode.unisop.client.info.SopTokenInfo;
import licode.unisop.client.utils.SopException;
import licode.unisop.client.utils.SopRequestParse;
import licode.unisop.client.utils.StringUtil;
import licode.unisop.client.vo.SopAuthPlatform;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * APP认证接口定义
 *
 * @author WR
 */
@Component
public class UniClientTool {
    @Resource
    private SopPlatformMng platformMng;

    @Value("${uni-sop.platform:token-name}")
    private String tokenName;

    public SopPlatform findPlatform(HttpServletRequest request, String platId) {
        String platTag = platId;
        if (StringUtil.isBlank(platTag)) {
            platId = SopRequestParse.getValue(request, tokenName);;
        }

        SopPlatform platform = platformMng.getPlatform(platId);
        if (null == platform || platform.isValid()) {
            /*
             * 平台不可用（在认证中心没有注册）
             */
            throw new SopException(403, "无效平台！");
        }
        return platform;
    }

    public SopAuthPlatform findAuthPlatform(HttpServletRequest request, String platId) {
        SopPlatform platform = findPlatform(request, platId);
        return platform.getAuthPlatform();
    }

    public SopTokenInfo getAccessToken(HttpServletRequest request, String platId) {
        SopPlatform platform = findPlatform(request, platId);
        SopTokenInfo authToken = SopRequestParse.getAccessTokenInfo(request);
        authToken.setPlatformId(platform.getId());
        return authToken;
    }

    public SopTokenInfo getRefreshToken(HttpServletRequest request, String platId) {
        SopPlatform platform = findPlatform(request, platId);
        SopTokenInfo authToken = SopRequestParse.getRefreshTokenInfo(request);
        authToken.setRealmId(platform.getRealmId());
        authToken.setPlatformId(platform.getId());
        authToken.setToken(SopRequestParse.getRefreshToken(request));

        return authToken;
    }

    public void checkToken(String accessToken) {
        if (StringUtil.isBlank(accessToken)) {
            throw new SopException(403, "无效登录信息！");
        }
    }

    public SopTokenInfo getAuthToken(HttpServletRequest request, SopRealmIdIn idIn) {
        SopAuthPlatform platform = findAuthPlatform(request, idIn.getPlatformId());
        SopTokenInfo authToken = SopRequestParse.getAccessTokenInfo(request);

        if (StringUtil.isNotBlank(platform.getRealmId())) {
            authToken.setRealmId(platform.getRealmId());
        } else {
            authToken.setRealmId(idIn.getRealmId());
        }
        authToken.setPlatformId(platform.getPlatformId());
        checkToken(authToken.getToken());

        return authToken;
    }
}
